import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views_front/login/index')
  },

  {
    path: '/',
    component: () => import('@/views_front/index'),
    redirect: '/index',
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views_front/index')
      }
    ],
    hidden: true
  },

  {
    path: '/personalHomePage',
    component: () => import('@/views_front/user/index'),
    redirect: '/personalHomePage/info',
    children: [
      {
        path: '/personalHomePage/info', // 个人资料
        name: 'Info',
        component: () => import('@/views_front/userInfo/index')
      },
      {
        path: '/personalHomePage/changeInfo', // 账号设置
        name: 'ChangeInfo',
        component: () => import('@/views_front/userInfo/changeInfo')
      },
      {
        path: '/personalHomePage/articleList', // 我的文章
        name: 'ChangeAvatar',
        component: () => import('@/views_front/userInfo/article')
      },
      {
        path: '/personalHomePage/userFocus', // 我的关注
        name: 'UserFocus',
        component: () => import('@/views_front/userInfo/userFocus')
      },
      {
        path: '/personalHomePage/question', // 我的关注
        name: 'MyQuestion',
        component: () => import('@/views_front/userInfo/myQuestion')
      },
      {
        path: '/personalHomePage/collection', // 我的收藏
        name: 'Collection',
        component: () => import('@/views_front/userInfo/collection')
      }
    ],
    hidden: true
  },
  // 成为心理咨询师
  {
    path: '/to-be-consultant',
    component: () => import('@/views_front/consultant/consultant')
  },
  // 发布文章页面
  {
    path: '/publishArticle',
    component: () => import('@/views_front/markdown/index')
  },
  // 编辑文章页面
  {
    path: '/editArticle/:id',
    component: () => import('@/views_front/markdown/edit')
  },

  // 文章详情页
  {
    path: '/article/:id',
    name: 'ArticleDetail',
    component: () => import('@/views_front/article/detail')
  },
  // 问答详情页
  {
    path: '/question',
    name: 'QuestionList',
    component: () => import('@/views_front/question/questionList')
  },
  {
    path: '/question/:id',
    name: 'QuestionDetail',
    component: () => import('@/views_front/question/detail')
  },
  // 个人主页
  {
    path: '/user/:id',
    name: 'User',
    component: () => import('@/views_front/user/user')
  },
  // 问卷做题页
  {
    path: '/test/:id',
    name: 'DoTest',
    component: () => import('@/views_front/paper/detail')
  },
  // 心理咨询列表
  {
    path: '/consultation',
    name: 'Consultation',
    component: () => import('@/views_front/consultation/index'),
    children: [{
      path: ':id',
      name: 'TeacherList',
      component: () => import('@/views_front/consultation/index')
    }],
    hidden: true
  },
  // 消息列表
  {
    path: '/whisper',
    name: 'Whisper',
    component: () => import('@/views_front/chat/index'),
    children: [{
      path: '/whisper/myChats',
      component: () => import('@/views_front/chat/chat'),
      name: 'Chat'
    },
    {
      path: '/whisper/reply',
      name: 'Reply',
      component: () => import('@/views_front/chat/reply')
    }
    ],
    hidden: true
  },
  // 发布问答页面
  {
    path: '/addQuestion',
    component: () => import('@/views_front/question/addQuestion')
  },

  {
    path: '/articleList',
    component: () => import('@/views_front/article_list/article'),
    children: [
      {
        path: ':id',
        name: 'ArticleList',
        component: () => import('@/views_front/article_list/article')
      }
    ],
    hidden: true
  },

  // 以下是管理员页面

  {
    path: '/back/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/back',
    component: Layout,
    redirect: '/back/index',
    children: [{
      path: '/back/index',
      name: 'BackIndex',
      component: () => import('@/views/index/index'),
      meta: { title: '首页', icon: 'el-icon-s-home' }
    }]
  },

  {
    path: '/back/dashboard',
    component: Layout,
    redirect: '/dashboard',
    name: 'Dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '个人中心', icon: 'el-icon-user' }
      }
    ]
  },

  {
    path: '/back/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: '用户管理', icon: 'table' }
      }
    ]
  },

  {
    path: '/back/article',
    component: Layout,
    redirect: '/article/essay',
    name: 'Article',
    children: [
      {
        path: 'essay',
        name: 'Essay',
        component: () => import('@/views/article/index'),
        meta: { title: '文章列表', icon: 'el-icon-reading' }
      }
    ]
  },

  {
    path: '/back/application',
    component: Layout,
    name: 'Application',
    meta: { title: '申请管理', icon: 'application' },
    children: [
      {
        path: 'notAudit',
        name: 'NotAudit',
        component: () => import('@/views/application/index'),
        meta: { title: '未审核', icon: 'notAudit' }
      },
      {
        path: 'approv',
        component: () => import('@/views/application/approv'),
        name: 'Approv',
        meta: { title: '已审核', icon: 'approv' }
      }
    ]
  },

  {
    path: '/back/report',
    component: Layout,
    name: 'Report',
    meta: { title: '举报管理', icon: 'report' },
    children: [
      {
        path: 'articleReport',
        name: 'ArticleReport',
        component: () => import('@/views/report/article'),
        meta: { title: '文章', icon: 'notAudit' },
        children: [
          {
            path: 'article',
            name: 'ArticleTipOff',
            component: () => import('@/views/report/article/detail/report'),
            meta: { title: '文章举报', icon: 'notAudit' }
          },
          {
            path: 'comment',
            name: 'Comment',
            component: () => import('@/views/report/article/comment'),
            meta: { title: '评论举报', icon: 'notAudit' }
          }
        ]
      },
      {
        path: 'questionReport',
        component: () => import('@/views/report/question'),
        name: 'Qa',
        meta: { title: '问答', icon: 'approv' },
        children: [
          {
            path: 'contentReport',
            name: 'ContentReport',
            component: () => import('@/views/report/question/content'),
            meta: { title: '问题内容举报', icon: 'notAudit' }
          },
          {
            path: 'questionAnswerReport',
            name: 'QuestionAnswerReport',
            component: () => import('@/views/report/question/qa'),
            meta: { title: '回答举报', icon: 'notAudit' }
          },
          {
            path: 'questionAnswerReplyReport',
            name: 'QuestionAnswerReplyReport',
            component: () => import('@/views/report/question/reply'),
            meta: { title: '回答评论举报', icon: 'notAudit' }
          }
        ]
      }
    ]
  },

  {
    path: '/back/test',
    name: 'Test',
    component: Layout,
    meta: { title: '心理测试', icon: 'form' },
    children: [
      {
        path: 'paper',
        name: 'Paper',
        component: () => import('@/views/test/paper'),
        meta: { title: '问卷创建', icon: 'form' }
      },
      {
        path: 'paper-list',
        name: 'PaperList',
        component: () => import('@/views/test/paper-list/index'),
        meta: { title: '问卷浏览', icon: 'form' }
      },
      {
        path: ':id',
        name: 'PaperDetail',
        component: () => import('@/views/test/paper-list/detail.vue'),
        meta: { title: '问卷详情', icon: 'form' },
        hidden: true
      }
    ]
  },

  {
    path: '/back/question',
    component: Layout,
    redirect: '/question/tagManagement',
    name: 'Question',
    meta: {
      title: '问答',
      icon: 'nested'
    },
    children: [
      {
        path: 'tagManagement',
        component: () => import('@/views/question/tagManage'), // Parent router-view
        name: 'Menu1',
        meta: { title: '问答管理', icon: 'question' },
        children: [
          {
            path: 'tag1',
            component: () => import('@/views/question/tagManage/tag1'),
            name: 'Menu-1',
            meta: { title: '问题标签-1', icon: 'tag' }
          },
          {
            path: 'tag2',
            component: () => import('@/views/question/tagManage/tag2'),
            name: 'Menu1-2',
            meta: { title: '问题标签-2', icon: 'tag' }
          }
        ]
      },
      {
        path: 'tagList',
        component: () => import('@/views/question/tagList/index'),
        name: 'TagList',
        meta: { title: '问答列表', icon: 'nested' }
      }
    ]
  },

  // {
  //   path: 'external-link',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
  //       meta: { title: 'External Link', icon: 'link' }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
